package com.project.hqshorttaxi.service.impl;

import com.project.hqshorttaxi.dto.ParkingNodeParam;
import com.project.hqshorttaxi.entity.ParkingNode;
import com.project.hqshorttaxi.mapper.ParkingNodeMapper;
import com.project.hqshorttaxi.service.ParkingNodeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ParkingNodeServiceImpl implements ParkingNodeService {

    @Resource
    private ParkingNodeMapper parkingNodeMapper;

    @Override
    public ParkingNode getById(Integer id) {
        return parkingNodeMapper.selectByPrimaryKey(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insert(ParkingNode entity) throws Exception {
        parkingNodeMapper.insertSelective(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(ParkingNode entity) throws Exception {
        parkingNodeMapper.updateByPrimaryKeySelective(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(Integer id) throws Exception {
        parkingNodeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<ParkingNode> list(ParkingNodeParam param) {
        Example example = new Example(ParkingNode.class);
        Example.Criteria c = example.createCriteria();
        if (param.getParkingNo() != null) {
            c.andEqualTo("parkingNo", param.getParkingNo());
        }
        if (param.getInuse() != null) {
            c.andEqualTo("inuse", param.getInuse());
        }
        List<ParkingNode> list = parkingNodeMapper.selectByExample(example);
        return list;
    }
}
